package com.qyhl.webtv.module_microvideo.shortvideo.shoot.upload;

import android.annotation.SuppressLint;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.module_microvideo.common.VideoUrl;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ShortVideoUploadModel implements ShortVideoUploadContract.ShortVideoUploadModel {
    private ShortVideoUploadPresenter mPresenter;

    ShortVideoUploadModel(ShortVideoUploadPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getToken(final boolean isUpload) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = VideoUrl.CIRCLE_URL + "?timestamp=" + time;


        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=getUpToken&type=3"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean upTokenBean) {
                        mPresenter.getTokenSuccess(upTokenBean, isUpload);
                    }
                });
    }

    @Override
    public void upLoadData(String title, String videoUrl, String logo, String phone, String tagId, String actId) {

        EasyHttp.post(VideoUrl.POST_SHORT_VIDEO_WORK)
                .params("siteId", CommonUtils.getInstance().getSite()+ "")
                .params("title", title)
                .params("videoUrl", videoUrl)
                .params("logo", logo)
                .params("phone", phone)
                .params("tagId", tagId)
                .params("actId", actId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.upLoadError("上传失败，请重新尝试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult result = new Gson().fromJson(s, ApiResult.class);
                        if (result.getCode() == 200) {
                            if (result.getData().toString().equals("1")) {
                                mPresenter.upLoadSuccess("上传成功，请等待审核！");
                            } else {
                                mPresenter.upLoadSuccess("上传成功！");
                            }
                        } else {
                            mPresenter.upLoadError("上传失败，请重新尝试！");
                        }
                    }
                });


    }
}
